﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;

namespace DELVE_GAME.ViewStructure
{
    class MenuStage : Stage
    {
        List<MenuOption> menuOptions;
        
        static KeyboardState previousKeyboardState;
        static KeyboardState currentKeyboardState;
        Vector2 position;
        public String title;
        public SpriteFont titleFont;
        int optionIterator;
        SoundEffectInstance sound;
        
        

        public MenuStage(String name, SpriteFont font, int x, int y, SoundEffectInstance optionSoundEffect)
        {
            position = new Vector2(x, y);
            menuOptions = new List<MenuOption>();
            optionIterator = 0;

            sound = optionSoundEffect;
            
            title = name;
            titleFont = font;

            previousKeyboardState = Keyboard.GetState();
            currentKeyboardState = Keyboard.GetState();
        }

        #region Option Operations
        public int GetNumberOfOptions()
        {
            return menuOptions.Count;
        }

        public MenuOption GetOptionsItem(int index)
        {
            return menuOptions[index];
        }

        public int getOptionCount()
        {
            return menuOptions.Count;
        }
        #endregion

        #region Add items to list
        public void addOption(MenuOption menuItem)
        {
            menuOptions.Add(menuItem);
        }
        #endregion


        //called when input up/down
        public void setOptionSelection(int optionIterator)
        {
            for (int k = 0; k < getOptionCount(); k++)
            {
                if (k == optionIterator)
                    menuOptions[k].setSelection(true);
                else
                    menuOptions[k].setSelection(false);
            }

        }

        public override void update()
        {
            //update key presses
            previousKeyboardState = currentKeyboardState;
            currentKeyboardState = Keyboard.GetState();

            handleInput();

            foreach (ViewComponent c in components)
            {
                c.update();
            }

            // draw all of your options
            foreach (MenuOption i in menuOptions)
            {
                i.update();
            }

            setOptionSelection(optionIterator);
            
        }

        //sets state of keyboard for key requested
        public bool keyTapped(Keys key)
        {
            return currentKeyboardState.IsKeyDown(key) && previousKeyboardState.IsKeyUp(key);
        }

        //write input actions in this class
        private void handleInput()
        {
            int skipCount = 0;

            if (keyTapped(Keys.Enter))
            {
                //do action if MenuItem has a confirm action
                menuOptions[optionIterator].doEnterAction();
                //do action of Menu choice for menu option
                if (menuOptions[optionIterator].getChoiceCount() > 0)
                {
                    menuOptions[optionIterator].menuChoices[menuOptions[optionIterator].choiceIterator].doEnterAction();
                }
                //GetOptionsItem(optionIterator).setChoiceSelection(GetOptionsItem(optionIterator).choiceIterator);
            }

            else if (keyTapped(Keys.Up))
            {
                //skipCount = optionIterator;
                while(menuOptions[skipCount].getVisibility() == false && skipCount > 0 )
                {
                    skipCount--;
                }

                if ((optionIterator + skipCount - 1) >= 0)
                {
                    //move to next available option, else can't move up list
                    optionIterator = (optionIterator + skipCount - 1);
                    sound.Play(); 
                }
            }

            else if (keyTapped(Keys.Down))
            {
                //skipCount = optionIterator;
                while(menuOptions[skipCount].getVisibility() == false && skipCount < getOptionCount() - 1 )
                {
                    skipCount++;
                }

                if ((optionIterator + skipCount + 1) <= getOptionCount() - 1)
                {
                    //move to next available option, else can't move down list
                    optionIterator = (optionIterator + skipCount + 1);
                    sound.Play();
                }
            }
            
          
            else if (keyTapped(Keys.Left))
            {
                if (GetOptionsItem(optionIterator).choiceIterator > 0)
                {
                    GetOptionsItem(optionIterator).choiceIterator--;
                    GetOptionsItem(optionIterator).setChoiceSelection(GetOptionsItem(optionIterator).choiceIterator);
                    sound.Play();
                }
            }

            else if (keyTapped(Keys.Right))
            {
                if (GetOptionsItem(optionIterator).choiceIterator < GetOptionsItem(optionIterator).getChoiceCount() - 1)
                {
                    GetOptionsItem(optionIterator).choiceIterator++;
                    GetOptionsItem(optionIterator).setChoiceSelection(GetOptionsItem(optionIterator).choiceIterator);
                    sound.Play();
                }
            }

            previousKeyboardState = currentKeyboardState;
            
            
        }

        public override void draw(SpriteBatch sb)
        {
            /* Hey Christian, I took the liberty
             * of adding these few lines of code
             * so that you can see your menus.
             *              :)
             */

            // makes sure that the background gets drawn
            base.draw(sb);
            sb.Begin();
            sb.DrawString(titleFont, this.title, new Vector2(position.X - titleFont.MeasureString(this.title).X / 2, position.Y), Color.White);
            sb.End();

            // draw all the components
            foreach (ViewComponent c in components)
            {
                c.draw(sb);
            }

            // draw all of your options
            foreach (MenuOption i in menuOptions)
            {
                i.draw(sb);

                foreach (MenuChoice c in i.menuChoices)
                {
                    c.draw(sb);
                }
            }
        }

    }
}
